﻿using System.Threading;
using System.Collections.Generic;

namespace Framework
{
    public class Channel<T>
    {
        private ConcurrentQueue<T> m_queue;
        private Semaphore m_semaphore;
        
        public Channel()
        {
            m_queue = new ConcurrentQueue<T>();
            m_semaphore = new Semaphore(0, int.MaxValue);
        }

        ~Channel()
        {
            m_semaphore.Close();
        }

        public void Enqueue(T item)
        {
            m_queue.Enqueue(item);
            m_semaphore.Release();
        }

        public T Dequeue()
        {
            m_semaphore.WaitOne();
            T item = m_queue.Dequeue();
            return item;
        }
    }
}

